package com.cy.tree;

/**
 * @Author: cy
 * @Date: 2021/09/26/15:20
 * @Description:
 * 递归方法：先序、中序、后序遍历树
 */
public class Demo1 {
    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        Node node7 = new Node(7);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        System.out.println("先序遍历顺序是：");
        recursivePreOrder(node1); //先序遍历
        System.out.println();
        System.out.println("中序遍历顺序是：");
        recursiveInOrder(node1);
        System.out.println();
        System.out.println("后序遍历顺序是：");
        recursivePostOrder(node1);

    }

    //先序遍历  根 左  右
    public static void recursivePreOrder(Node head){
        if(head == null){
            return;
        }
        System.out.print(head.value+" ");
        recursivePreOrder(head.left);
        recursivePreOrder(head.right);
    }

    //中序遍历
    public static void recursiveInOrder(Node head){
        if(head == null){
            return;
        }
        recursiveInOrder(head.left);
        System.out.print(head.value+" ");
        recursiveInOrder(head.right);
    }

    //后序遍历
    public static void recursivePostOrder(Node head){
        if(head == null){
            return;
        }
        recursivePostOrder(head.left);
        recursivePostOrder(head.right);
        System.out.print(head.value+" ");
    }
}

